package com.huawei.hgip.paintgame.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.huawei.hgip.paintgame.R;
import com.huawei.hgip.paintgame.activity.AppManageActivity;
import com.huawei.hgip.paintgame.activity.HistoryActivity;
import com.huawei.hgip.paintgame.activity.MainActivity;
import com.huawei.hgip.paintgame.m.data.UserProfile;
import com.huawei.hgip.paintgame.util.DBAdapter;
import com.huawei.hgip.paintgame.widget.PictureLayout;

/**
 * 在这个类中实现一些最基本的东西
 * 比如软件整体的风格，menu菜单的集中管理
 * @author Administrator
 *
 */
public class ZZYYActivity extends Activity {
	
	protected DBAdapter database;
	
    //@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        database = DBAdapter.getInstance(this);
    }

    //@Override
    @SuppressWarnings("unused")
	public void setContentView(View view) {
        if (false) { // set to true to test Picture
            ViewGroup vg = new PictureLayout(this);
            vg.addView(view);
            view = vg;
        }
        
        super.setContentView(view);
    }
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, "home".hashCode(), 10, "首页").setIcon(
			R.drawable.ic_menu_home);
		menu.add(0, "login".hashCode(), 11, "登录").setIcon(
			R.drawable.ic_menu_login);
		menu.add(0, "appmanage".hashCode(), 20, "应用管理").setIcon(
			android.R.drawable.ic_menu_preferences);
		menu.add(0, "history".hashCode(), 21, "历史").setIcon(
			R.drawable.ic_menu_history);
		return super.onCreateOptionsMenu(menu);
	}
	
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.findItem("home".hashCode()).setVisible(
			!(this instanceof MainActivity));
		menu.findItem("appmanage".hashCode()).setVisible(
			this instanceof MainActivity);
		menu.findItem("login".hashCode()).setVisible(false);
		menu.findItem("history".hashCode()).setVisible(true);
		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == "home".hashCode()) {
			Intent i = new Intent(this, MainActivity.class);
			i.putExtra("isLogin", true);
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			this.finish();
			return true;
		}
		if (item.getItemId() == "history".hashCode()) {
			Intent i = new Intent(this, HistoryActivity.class);
			startActivity(i);
			return true;
		}
		if (item.getItemId() == "appmanage".hashCode()) {
			Intent i = new Intent(this, AppManageActivity.class);
			startActivity(i);
		}
		return super.onOptionsItemSelected(item);
	}
	
	/**
	 * 获取用户账户，若为null，表示未登录
	 * @return
	 */
	public UserProfile getAccount() {
		return null;
	}

	/**
	 * 
	 * @param exceType 异常信息
	 */
	public void popupException(String exceType){		
		Toast.makeText(this, exceType, Toast.LENGTH_LONG).show();
	}
}
